package com.crazyhat.crazyhat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crazyhat.crazyhat.attribute.ApprovalStatusEnum;
import com.crazyhat.crazyhat.dto.ClubApplicationDTO;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.service.IApplicationService;

/**
 * <p>
 * 社团申请 前端控制器
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Autowired
    private IApplicationService applicationService;

    /**
     * 申请加入社团
     * @param joinClubId 申请加入社团的ID
     * @return
     */
    @PostMapping("/joinClub")
    public Result joinClub(@RequestParam("clubId") Integer joinClubId ) {

        ApprovalStatusEnum approvalStatusEnum=applicationService.joinClub(joinClubId);

        if (approvalStatusEnum!=null) {
            return Result.success(approvalStatusEnum);
        } else {
            return Result.error("申请失败");
        }
    }
    

    /**
     * 审核人员申请
     */
    @PostMapping("/approveClub")
    public Result checkApplication(@RequestParam Integer applicationId, @RequestParam boolean approve) {
        boolean success = applicationService.checkApplication(applicationId, approve);
        if (success) {
            return Result.success("操作成功");
        } else {
            return Result.error("操作失败");
        }
    }

    
    /**
     * 获取社团的申请列表
     * @param clubId
     * @param getAll 是否获取全部状态的申请，是：true ，否：false
     * @return
     */
    @GetMapping("/ApplicationList")
    public Result getApplicationList(
        @RequestParam("clubId") Integer clubId,
        @RequestParam(value = "getAll",required = false) Boolean getAll) {

            if (getAll==null) {
                getAll=false;
            }

        List<ClubApplicationDTO> applicationList= applicationService.getApplicationList(clubId,getAll);

        if (applicationList!=null) {
            return Result.success(applicationList);
        } else {
            return Result.error("获取社团加入申请列表失败");
        }
    }
}
